package presentation;

import java.util.Observable;
import java.util.Observer;

import control.CControl;

public class PBatteryVoltage  extends Observable implements Observer{
	
	
	private double[][] bank_cell_voltages;
	
	PBattReg dump1;
	PBattReg dump2;
	PBattReg dump3;
	PBattReg dump4;
	PBattReg dump5;
	
	public PBatteryVoltage(CControl c)
	{
		
		bank_cell_voltages = new double [6][18];
		int  k = 0;
		for(int i = 0; i < 6;i++)
		{
			for (int j=0;j<18;j++)
			{
				bank_cell_voltages[i][j] = 0;
			}
		}
		
		dump1 = new PBattReg();
		dump2 = new PBattReg();
		dump3 = new PBattReg();
		dump4 = new PBattReg();
		dump5 = new PBattReg();
		
		
		c.addRegisterObserver("MBN_BMS_VoltageDump1", dump1);
		c.addRegisterObserver("MBN_BMS_VoltageDump2", dump2);
		c.addRegisterObserver("MBN_BMS_VoltageDump3", dump3);
		c.addRegisterObserver("MBN_BMS_VoltageDump4", dump4);
		c.addRegisterObserver("MBN_BMS_VoltageDump5", dump5);
		
		c.addStreamObserver(this);
		
		
	}
	
	public void test()
	{
		System.out.println("Emiiting start");
		setChanged();
		notifyObservers();
		System.out.println("Emiiting end");
	}

	public double[] get_bank(int nr)
	{
		return bank_cell_voltages[nr];
	}
	
	private void update16bank(int nr)
	{
		
		int offset = 0;
		for(int i = 0; i<4;i++)
		{
			bank_cell_voltages[nr][i+offset] = (double)( (dump2.battreg & (0xFF << (i*8))) >> (i*8)) * 0.01 + 2;
		}
		offset += 4;
		for(int i = 0; i<4;i++)
		{
			bank_cell_voltages[nr][i+offset] = (double)( (dump3.battreg & (0xFF << (i*8))) >> (i*8)) * 0.01 + 2;
		}
		offset += 4;
		for(int i = 0; i<4;i++)
		{
			bank_cell_voltages[nr][i+offset] = (double)( (dump4.battreg & (0xFF << (i*8))) >> (i*8)) * 0.01 + 2;
		}
		offset += 4;
		for(int i = 0; i<4;i++)
		{
			bank_cell_voltages[nr][i+offset] = (double)( (dump5.battreg & (0xFF << (i*8))) >> (i*8)) * 0.01 + 2;
		}
		
	}
	
	private void update18bank(int nr)
	{
		int offset = 0;
		for(int i = 0; i<4;i++)
		{
			bank_cell_voltages[nr][i+offset] = (double)( (dump2.battreg & (0xFF << (i*8))) >> (i*8)) * 0.01 + 2;
		}
		offset += 4;
		for(int i = 0; i<4;i++)
		{
			bank_cell_voltages[nr][i+offset] = (double)( (dump3.battreg & (0xFF << (i*8))) >> (i*8)) * 0.01 + 2;
		}
		offset += 4;
		for(int i = 0; i<4;i++)
		{
			bank_cell_voltages[nr][i+offset] = (double)( (dump4.battreg & (0xFF << (i*8))) >> (i*8)) * 0.01 + 2;
		}
		offset += 4;
		for(int i = 0; i<4;i++)
		{
			bank_cell_voltages[nr][i+offset] = (double)( (dump5.battreg & (0xFF << (i*8))) >> (i*8)) * 0.01 + 2;
		}
		offset += 4;
		bank_cell_voltages[nr][0+offset] = (double)((dump1.battreg & (0x000000FF))) * 0.01 + 2;
		bank_cell_voltages[nr][1+offset] = (double)((dump1.battreg & (0x0000FF00)) >> 8) * 0.01 + 2;
		
	}
	
	@Override
	public void update(Observable o, Object arg) {
		// TODO Auto-generated method stub
		// readvalue from each dump and update correct entry in cellarray
		int index = (int)((dump1.battreg & (0xFF000000)) >> 24);
		switch (index) {
		case 1:
			// voltages are fr bank 1 (cell 0 to 15)
			update16bank(0);
			break;
		case 2:
			// voltages are fr bank 2 (cell 16 to 31)
			update16bank(1);
			break;
		case 3:
			// voltages are fr bank 3 (cell 32 to 47)
			update18bank(2);
			break;
		case 4:
			// voltages are fr bank 3 (cell 48 to 63)
			update18bank(3);
			break;
		case 5:
			// voltages are fr bank 5 (cell 64 to 81) (18 cells)
			update16bank(4);
			break;
		case 6:
			// voltages are fr bank 5 (cell 82 to 99) (18 cells)
			update16bank(5);
			break;
		default:
			break;
		}
		setChanged();
		notifyObservers();
		

	}

}

